package top.lbqaq.service;

import top.lbqaq.dao.BaseDao;
import top.lbqaq.dao.SupplierDao;
import top.lbqaq.pojo.Supplier;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

/**
 * @author luoboQAQ
 * @Date 2021/11/30
 */
public class SupplierService {

    private final SupplierDao supplierDao = new SupplierDao();

    public List<Supplier> queryAll() {
        Connection connection = null;
        connection = BaseDao.getConnection();
        List<Supplier> suppliers = null;
        try {
            suppliers = supplierDao.queryAll(connection);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            BaseDao.closeResource(connection, null, null);
        }
        return suppliers;
    }

    public List<Supplier> queryBySname(String sname) {
        Connection connection = null;
        connection = BaseDao.getConnection();
        List<Supplier> suppliers = null;
        try {
            suppliers = supplierDao.queryBySname(connection, sname);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            BaseDao.closeResource(connection, null, null);
        }
        return suppliers;
    }

    public List<Supplier> queryBySupply(String supply) {
        Connection connection = null;
        connection = BaseDao.getConnection();
        List<Supplier> suppliers = null;
        try {
            suppliers = supplierDao.queryBySupply(connection, supply);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            BaseDao.closeResource(connection, null, null);
        }
        return suppliers;
    }

    public int insert(Supplier supplier) {
        Connection connection = null;
        connection = BaseDao.getConnection();
        int end = 0;
        try {
            end = supplierDao.insert(connection, supplier);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            BaseDao.closeResource(connection, null, null);
        }
        return end;
    }

    public int updateBySid(Supplier supplier) {
        Connection connection = null;
        connection = BaseDao.getConnection();
        int end = 0;
        try {
            end = supplierDao.updateBySid(connection, supplier);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            BaseDao.closeResource(connection, null, null);
        }
        return end;
    }

    public Supplier queryBySid(String sid) {
        Connection connection = null;
        connection = BaseDao.getConnection();
        Supplier supplier = null;
        try {
            supplier = supplierDao.queryBySid(connection, sid);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            BaseDao.closeResource(connection, null, null);
        }
        return supplier;
    }

    public int deleteBySid(String sid) {
        Connection connection = null;
        connection = BaseDao.getConnection();
        int end = 0;
        try {
            end = supplierDao.deleteBySid(connection, sid);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            BaseDao.closeResource(connection, null, null);
        }
        return end;
    }

    public int getCount(String sname, String supply) {
        Connection connection = null;
        connection = BaseDao.getConnection();
        int end = 0;
        try {
            end = supplierDao.getCount(connection, sname, supply);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            BaseDao.closeResource(connection, null, null);
        }
        return end;
    }

    public List<Supplier> getList(String sname, String supply, int currentPageNo, int pageSize) {
        Connection connection = null;
        connection = BaseDao.getConnection();
        List<Supplier> suppliers = null;
        try {
            suppliers = supplierDao.getList(connection, sname, supply, currentPageNo, pageSize);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            BaseDao.closeResource(connection, null, null);
        }
        return suppliers;
    }
}
